package com.ophiux.alipay.oauth;

import net.sf.json.JSONObject;

import com.ophiux.common.utils.CommonUtil;

public class OauthUtil {
	private static String OAUTH_URL = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=APP_ID&secret=APP_SECRET&code=CODE&grant_type=authorization_code";

	public static String getWxOpenID(String appid, String appsecret, String code) {
		String url = OAUTH_URL.replace("APP_ID", appid)
				.replace("APP_SECRET", appsecret).replace("CODE", code);

		String tempValue = "";
		String openId = "";
		String oauResp = "";
		try {
			oauResp = CommonUtil.httpsRequest(OAUTH_URL, CommonUtil.HTTP_GET,
					"");
		} catch (Exception e) {
			e.printStackTrace();
		}
		JSONObject jsonObj = JSONObject.fromObject(oauResp);
		if (jsonObj.containsKey("errcode")) {
			System.out.println(tempValue);
			openId = "";
		}
		openId = jsonObj.getString("openid");

		return openId;
	}
}